//
//  QSBaseViewController.swift
//  TestSwift
//
//  Created by Song on 2018/4/29.
//  Copyright © 2018 Song. All rights reserved.
//

import UIKit
import ETNavBarTransparent

class QSBaseViewController: UIViewController {
    // MARK: - Life Cycle
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // 默认不使用大标题
        self.qs_useNavLargeTitle(false)
        // 设置导航栏的title的字体和颜色
        self.qs_setNavTitle(font: UIFont.qs_otherFont(fontName: kMediumFontName, size: 18.0), textColor: kColor_4C4C4C)
        // 设置导航栏大标题的字体和颜色
        self.qs_setNavLargeTitle(font: nil)
        // 默认不需要导航栏的黑线
        self.qs_setNavBarShadowImage(isHidden: true, color: nil)
        // 隐藏TabBar上的黑线
        self.qs_setTabBarShadowImage(isHidden: true)
        // 默认不穿透导航栏
        self.qs_setExtendNavBar(isExtend: false)
        // 默认不隐藏导航栏
        self.qs_hideNavBar(false, animated: true)
        // 设置导航栏背景颜色
        self.qs_setNavBarBgColor(.white)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        if let nav = self.navigationController {
            nav.interactivePopGestureRecognizer?.isEnabled = nav.childViewControllers.count > 1
        }
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        
        view.endEditing(true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .white
        // 设置返回按钮
        if let nav = self.navigationController {
            if nav.children.count > 1 {
                // 设置返回按钮
                let backItem = UIBarButtonItem.qs_imgAndTitleBtnItem(title: "    ", img: "nav_back", titleColor: .white, target: self, action: #selector(self.qs_back))
                self.navigationItem.setLeftBarButton(backItem, animated: true)
                nav.interactivePopGestureRecognizer?.delegate = self
            }
        }
        
        // 添加子控件
        self.qs_setupSubViews()
        // 绑定
        self.qs_binding()
    }
    
    // MARK: - Private Methods
    /// 设置子控件
    func qs_setupSubViews() {}
    
    /// 绑定
    func qs_binding() {}
    
    /// 返回pop
    @objc func qs_back() {
        self.navigationController?.popViewController(animated: true)
    }
}
